<template>
  <div class="quiz-competition-container">
    <div class="page-header">
      <div class="title-wrapper">
        <h1>【竞赛】</h1>
        <h2>知识的要全题挑战</h2>
      </div>
    </div>

    <div class="quiz-cards">
      <div class="cards-grid">
        <div
          v-for="(quiz, index) in quizzes"
          :key="quiz.id || ''"
          class="quiz-card"
          :class="`theme-${(index % 6) + 1}`"
        >
          <!-- 装饰角 -->
          <div class="corner-decoration top-left"></div>
          <div class="corner-decoration top-right"></div>
          <div class="corner-decoration bottom-left"></div>
          <div class="corner-decoration bottom-right"></div>

          <div class="card-content">
            <div class="card-left">
              <div class="card-title">{{ quiz.title || "" }}</div>
              <div class="card-description">
                {{ quiz.description || "" }}
              </div>
            </div>
            <div class="card-right">
              <div class="card-image">
                <img :src="quiz.imageUrl || ''" alt="竞赛图片" />
                <!-- 装饰印章 -->
                <div class="image-seal">
                  <span>竞赛</span>
                </div>
              </div>
              <a-button
                class="quiz-btn"
                type="primary"
                @click="quiz.id && goToQuizDetail(quiz.id)"
              >
                前往答题
              </a-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="pagination-container">
      <a-pagination
        v-model:current="currentPage"
        :total="total"
        :pageSize="pageSize"
        show-quick-jumper
        @change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { listChallengesUsingGet } from "@/api/activityQuestionController";
import type API from "@/api";

interface QuizChallenge {
  id?: number;
  activityId?: number;
  title?: string;
  description?: string;
  imageUrl?: string;
  status?: number;
  startTime?: string;
  endTime?: string;
}

const router = useRouter();
const quizzes = ref<QuizChallenge[]>([]);
const currentPage = ref(1);
const pageSize = ref(6); // 修改为6个卡片
const total = ref(0);

const loadQuizzes = async () => {
  try {
    const res = await listChallengesUsingGet({
      pageNum: currentPage.value,
      pageSize: pageSize.value,
    });
    if (res.data?.code === 0 && res.data.data?.records) {
      quizzes.value = res.data.data.records;
      total.value = Number(res.data.data.total) || 0;
    } else {
      message.error(res.data?.message || "加载知识竞赛列表失败");
    }
  } catch (error) {
    console.error("加载知识竞赛列表失败:", error);
    message.error("加载知识竞赛列表失败");
  }
};

const handlePageChange = (page: number) => {
  currentPage.value = page;
  loadQuizzes();
};

const goToQuizDetail = (id: number | undefined) => {
  if (id !== undefined) {
    router.push({ name: "quiz-answer", params: { id: String(id) } });
  }
};

onMounted(() => {
  loadQuizzes();
});
</script>

<style scoped>
.quiz-competition-container {
  width: 100%;
  min-height: 100vh;
  padding: 8vh 5vw;
  background: url("@/assets/background/dark-pattern.png") no-repeat center top;
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4vh;
}

.quiz-competition-container::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.2),
    rgba(0, 0, 0, 0.1)
  );
  z-index: 1;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  position: relative;
  z-index: 2;
}

.title-wrapper {
  display: inline-block;
}

.title-wrapper h1 {
  font-size: 32px;
  color: #e8d5b5;
  margin-bottom: 8px;
}

.title-wrapper h2 {
  font-size: 24px;
  color: #c4a484;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 40px;
  max-width: 1100px;
  margin: 0 auto;
  position: relative;
  z-index: 2;
}

.quiz-card {
  position: relative;
  height: 200px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(210, 180, 140, 0.3);
}

/* 主题颜色 */
.theme-1 {
  background: linear-gradient(135deg, #a89277 0%, #886f52 100%);
}

.theme-2 {
  background: linear-gradient(135deg, #b78956 0%, #9e7142 100%);
}

.theme-3 {
  background: linear-gradient(135deg, #9a6639 0%, #7a4626 100%);
}

.theme-4 {
  background: linear-gradient(135deg, #73380f 0%, #5d2e15 100%);
}

.theme-5 {
  background: linear-gradient(135deg, #b36d2c 0%, #95571e 100%);
}

.theme-6 {
  background: linear-gradient(135deg, #c09d6d 0%, #a47f4e 100%);
}

.quiz-card:hover {
  transform: translate(-8px, -8px);
  box-shadow: 12px 12px 20px rgba(0, 0, 0, 0.3);
}

/* 装饰角 */
.corner-decoration {
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(210, 180, 140, 0.6);
  z-index: 1;
}

.top-left {
  top: 8px;
  left: 8px;
  border-right: none;
  border-bottom: none;
}

.top-right {
  top: 8px;
  right: 8px;
  border-left: none;
  border-bottom: none;
}

.bottom-left {
  bottom: 8px;
  left: 8px;
  border-right: none;
  border-top: none;
}

.bottom-right {
  bottom: 8px;
  right: 8px;
  border-left: none;
  border-top: none;
}

.card-content {
  display: flex;
  height: 100%;
  position: relative;
  z-index: 2;
}

.card-left {
  flex: 1;
  padding: 24px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.card-title {
  font-size: 20px;
  color: #ffffff;
  font-weight: bold;
  margin-bottom: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.card-description {
  font-size: 14px;
  color: #ffffff;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-right {
  width: 140px;
  display: flex;
  flex-direction: column;
  padding: 16px 16px 16px 0;
}

.card-image {
  position: relative;
  height: 120px;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 装饰印章 */
.image-seal {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  background: #e74c3c;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: rotate(12deg);
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.image-seal::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  right: 2px;
  bottom: 2px;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 50%;
}

.image-seal span {
  color: white;
  font-size: 12px;
  font-weight: bold;
}

.quiz-btn {
  margin-top: auto;
  margin: 16px 10px 0;
  background: #e74c3c;
  border: none;
  color: white;
  font-weight: bold;
  height: 36px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.quiz-btn:hover {
  background: #c0392b;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.pagination-container {
  margin-top: 40px;
  text-align: center;
  position: relative;
  z-index: 2;
}

@media (max-width: 768px) {
  .cards-grid {
    grid-template-columns: 1fr;
  }

  .page-header h1 {
    font-size: 24px;
  }

  .page-header h2 {
    font-size: 18px;
  }
}
</style>
